import java.sql.*;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
        // 驱动类
        // DriverManager 自动加载META-INFO/services/java.sql.Driver文件
        Connection conn = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2. 创建链接
            //  协议  ip, 端口, 数据库， 用户名， 密码
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dl2545", "root", "guo147258");

            // 3. 创建执行器statement
            Statement stmt = conn.createStatement();


            // 4. 执行SQL
            // 不要有分号；
            String sql = "select * from emp_mst";
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                int emp_id = rs.getInt("id");
                String emp_name = rs.getString("emp_name");
                String password = rs.getString("password");
                String username = rs.getString("user_name");
                String sex = rs.getString(5);
                int emp_age = rs.getInt(6);

                System.out.println(emp_id + "@" + emp_name);

            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                try {
                    // 5. 关闭资源
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }


    }
}